package models

import (
	"lottery/libs/mymgo"

	"github.com/astaxie/beego/logs"
	"gopkg.in/mgo.v2/bson"
)

const (
	AGENT_COL_STR = "agent"
)

type Agent struct {
	Id          int64  `json:"id" bson:"_id"`
	LoginName   string `json:"loginName" bson:"loginName"`
	Passwd      string `json:"passwd" bson:"passwd"`
	Balance     int    `json:"balance" bson:"balance"`       // 单位分
	PlatformId  int    `json:"platformId" bson:"platformId"` // 平台ID
	UserId      int64  `json:"userId" bson:"userId"`
	Type        int    `json:"type" bson:"type"`           // 0 正常账号 1 补单账号
	IsLogin     bool   `json:"isLogin" bson:"isLogin"`     //是否登录在线
	TickTimes   int64  `json:"tickTimes" bson:"tickTimes"` // 点水次数
	Status      int    `json:"status" bson:"status"`       // 0 开始  1暂停
	IsDeleted   bool   `json:"isDeleted" bson:"isDeleted"`
	CreateTime  int64  `json:"createTime" bson:"createTime"`
	UpdateTime  int64  `json:"updateTime" bson:"updateTime"`
	DeletedTime int64  `json:"deletedTime" bson:"deletedTime"`
}

func (a *Agent) SetLoginStatus(status bool) {
	a.IsLogin = status
	sess, err := mymgo.OpenBase()
	if err != nil {
		logs.Warn("%d  设置登录状态失败 err:%s", a.Id, err.Error())
		return
	}

	if err = sess.UpdateId(AGENT_COL_STR, a.Id, bson.M{"$set": bson.M{
		"isLogin": a.IsLogin,
	}}); err != nil {
		logs.Warn("%d  设置登录状态失败 err:%s", a.Id, err.Error())
	}
	return
}

func (a *Agent) TickReset() {
	a.TickTimes = 0
	sess, err := mymgo.OpenBase()
	if err != nil {
		logs.Warn("%d  设置登录状态失败 err:%s", a.Id, err.Error())
		return
	}

	if err = sess.UpdateId(AGENT_COL_STR, a.Id, bson.M{"$set": bson.M{
		"tickTimes": a.TickTimes,
	}}); err != nil {
		logs.Warn("%d  设置登录状态失败 err:%s", a.Id, err.Error())
	}
	return
}

func (a *Agent) Pause() error {
	return a.setStatus(1)
}

func (a *Agent) Active() error {
	return a.setStatus(0)
}

func (a *Agent) setStatus(status int) (err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}

	return sess.UpdateId(AGENT_COL_STR, a.Id, bson.M{"$set": bson.M{"status": status}})
}

func (a *Agent) UpdateBalance() (err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}

	return sess.UpdateId(AGENT_COL_STR, a.Id, bson.M{"$set": bson.M{"balance": a.Balance}})
}

func (a *Agent) TickInc() {
	a.TickTimes++
	sess, err := mymgo.OpenBase()
	if err != nil {
		logs.Warn("%d  设置登录状态失败 err:%s", a.Id, err.Error())
		return
	}

	if err = sess.UpdateId(AGENT_COL_STR, a.Id, bson.M{
		"$inc": bson.M{"tickTimes": 1},
	}); err != nil {
		logs.Warn("%d  设置登录状态失败 err:%s", a.Id, err.Error())
	}
	return
}

func (a *Agent) Alive() bool {
	return a.Status == 0
}

func GetUserAgents(userId int64) (agents []*Agent, err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}

	agents = make([]*Agent, 0, 100)
	err = sess.All(AGENT_COL_STR, bson.M{"isDeleted": false, "status": 0, "userId": userId}, &agents)
	return
}

func SaveAgent(agent *Agent) (err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}

	agent.Id = int64(sess.AutoIncId(AGENT_COL_STR))
	return sess.Insert(AGENT_COL_STR, agent)
}

func ExistsLoginName(loginName string, platformId int) (exist bool) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	return sess.Exist(AGENT_COL_STR, bson.M{"loginName": loginName, "platformId": platformId})
}
func ExistsAgentId(userId int64, agentId int64) (exist bool) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	return sess.Exist(AGENT_COL_STR, bson.M{"_id": agentId, "userId": userId})
}
func UpdateAgent(agent *Agent) (err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	return sess.Update(AGENT_COL_STR, bson.M{"loginName": agent.LoginName, "isDeleted": false}, &agent)
}

func DelAgent(agent *Agent) (err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}

	err = sess.RemoveId(AGENT_COL_STR, agent.Id)
	return
}

func GetFreeAgent(platformId int, userId int64) (agent *Agent, err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	agent = &Agent{}
	err = sess.One(AGENT_COL_STR, bson.M{"platformId": platformId, "userId": userId, "isLogin": true, "status": 0}, agent)
	return
}

func GetAgentOne(agentId int64) (agent *Agent, err error) {
	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}
	agent = new(Agent)
	err = sess.FindId(AGENT_COL_STR, agentId, agent)
	return
}

func UpdataAllCount(userId int64, t int) (err error) {

	sess, err := mymgo.OpenBase()
	if err != nil {
		return
	}

	err = sess.UpdateAll(AGENT_COL_STR, bson.M{"userId": userId}, bson.M{"$set": bson.M{"status": t}})
	return
}
